package lab2;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.DropMode;
import javax.swing.JButton;
import javax.swing.border.TitledBorder;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;

import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainApp extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 4629599408486219240L;
    private JPanel contentPane;
    private static JTextArea input;
    static JTextField lblFactoroutput;
    static JTextField labelCPUOutput;
    static JTextField labelRhoCyclesOutput;
    static JTextField labelTimeOutput;
    private static JTextField txtMasterPort;
    private JTextField txtWorkerAdress;
    private JTextField txtWorkerPort;
    private JTextArea txtAreaWorkerList;
    private static List<String> workerList;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainApp frame = new MainApp();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MainApp() {
        workerList = new ArrayList<String>();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1066, 393);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu mnFile = new JMenu("File");
        menuBar.add(mnFile);

        final JMenuItem mntmExit = new JMenuItem("Exit");
        mntmExit.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent arg0) {
                Runtime.getRuntime().exit(EXIT_ON_CLOSE);
            }
        });
        mnFile.add(mntmExit);

        JMenu mnAbout = new JMenu("About");
        menuBar.add(mnAbout);

        JMenuItem mntmInfo = new JMenuItem("Info");
        mnAbout.add(mntmInfo);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel inputPanel = new JPanel();
        inputPanel.setBorder(new TitledBorder(null, "Prime Input", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        inputPanel.setBounds(6, 21, 927, 118);
        contentPane.add(inputPanel);
        inputPanel.setLayout(null);

        input = new JTextArea();
        input.setBounds(10, 22, 769, 69);
        inputPanel.add(input);
        input.setLineWrap(true);
        input.setDropMode(DropMode.INSERT);

        JButton btnStart = new JButton("Start");
        btnStart.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                if (workerList.size() == 0) {
                    JOptionPane.showMessageDialog(null, "Please add workers to list", null, JOptionPane.WARNING_MESSAGE);
                } else if (input.getText().equals("")) {
                    JOptionPane.showMessageDialog(null, "Please enter a valid number", null, JOptionPane.WARNING_MESSAGE);
                } else {
                    Master.start();
                }
            }
        });
        btnStart.setBounds(806, 43, 89, 23);
        inputPanel.add(btnStart);

        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Analysis Results", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0,
                0, 0)));
        panel.setBounds(6, 159, 459, 166);
        contentPane.add(panel);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[] { 80, 0, 40, 163, 0 };
        gbl_panel.rowHeights = new int[] { 31, 31, 31, 31, 0 };
        gbl_panel.columnWeights = new double[] { 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE };
        gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
        panel.setLayout(gbl_panel);

        JLabel lblFactor = new JLabel("Factor: ");
        lblFactor.setHorizontalAlignment(SwingConstants.LEFT);
        GridBagConstraints gbc_lblFactor = new GridBagConstraints();
        gbc_lblFactor.fill = GridBagConstraints.VERTICAL;
        gbc_lblFactor.anchor = GridBagConstraints.WEST;
        gbc_lblFactor.insets = new Insets(0, 0, 5, 5);
        gbc_lblFactor.gridx = 0;
        gbc_lblFactor.gridy = 0;
        panel.add(lblFactor, gbc_lblFactor);

        lblFactoroutput = new JTextField("");
        lblFactoroutput.setEditable(false);
        GridBagConstraints gbc_lblFactoroutput = new GridBagConstraints();
        gbc_lblFactoroutput.fill = GridBagConstraints.BOTH;
        gbc_lblFactoroutput.insets = new Insets(0, 0, 5, 0);
        gbc_lblFactoroutput.gridx = 3;
        gbc_lblFactoroutput.gridy = 0;
        panel.add(lblFactoroutput, gbc_lblFactoroutput);

        JLabel lblCpuTime = new JLabel("CPU time: ");
        GridBagConstraints gbc_lblCpuTime = new GridBagConstraints();
        gbc_lblCpuTime.fill = GridBagConstraints.BOTH;
        gbc_lblCpuTime.insets = new Insets(0, 0, 5, 5);
        gbc_lblCpuTime.gridx = 0;
        gbc_lblCpuTime.gridy = 1;
        panel.add(lblCpuTime, gbc_lblCpuTime);

        labelCPUOutput = new JTextField("0");
        labelCPUOutput.setEditable(false);
        GridBagConstraints gbc_labelCPUOutput = new GridBagConstraints();
        gbc_labelCPUOutput.fill = GridBagConstraints.BOTH;
        gbc_labelCPUOutput.insets = new Insets(0, 0, 5, 0);
        gbc_labelCPUOutput.gridx = 3;
        gbc_labelCPUOutput.gridy = 1;
        panel.add(labelCPUOutput, gbc_labelCPUOutput);

        JLabel lblRhocycles = new JLabel("Rho-Cycles: ");
        GridBagConstraints gbc_lblRhocycles = new GridBagConstraints();
        gbc_lblRhocycles.fill = GridBagConstraints.BOTH;
        gbc_lblRhocycles.insets = new Insets(0, 0, 5, 5);
        gbc_lblRhocycles.gridx = 0;
        gbc_lblRhocycles.gridy = 2;
        panel.add(lblRhocycles, gbc_lblRhocycles);

        labelRhoCyclesOutput = new JTextField("0");
        labelRhoCyclesOutput.setEditable(false);
        GridBagConstraints gbc_labelRhoCyclesOutput = new GridBagConstraints();
        gbc_labelRhoCyclesOutput.fill = GridBagConstraints.BOTH;
        gbc_labelRhoCyclesOutput.insets = new Insets(0, 0, 5, 0);
        gbc_labelRhoCyclesOutput.gridx = 3;
        gbc_labelRhoCyclesOutput.gridy = 2;
        panel.add(labelRhoCyclesOutput, gbc_labelRhoCyclesOutput);

        JLabel lblElapsedTime = new JLabel("Elapsed time: ");
        GridBagConstraints gbc_lblElapsedTime = new GridBagConstraints();
        gbc_lblElapsedTime.fill = GridBagConstraints.BOTH;
        gbc_lblElapsedTime.insets = new Insets(0, 0, 0, 5);
        gbc_lblElapsedTime.gridx = 0;
        gbc_lblElapsedTime.gridy = 3;
        panel.add(lblElapsedTime, gbc_lblElapsedTime);

        labelTimeOutput = new JTextField("");
        labelTimeOutput.setEditable(false);
        GridBagConstraints gbc_labelTimeOutput = new GridBagConstraints();
        gbc_labelTimeOutput.fill = GridBagConstraints.BOTH;
        gbc_labelTimeOutput.gridx = 3;
        gbc_labelTimeOutput.gridy = 3;
        panel.add(labelTimeOutput, gbc_labelTimeOutput);

        JLabel lblMasterPort = new JLabel("Master port: ");
        lblMasterPort.setBounds(477, 179, 128, 23);
        contentPane.add(lblMasterPort);

        JLabel lblWorkerAdress = new JLabel("Worker adress: ");
        lblWorkerAdress.setBounds(477, 215, 128, 16);
        contentPane.add(lblWorkerAdress);

        JLabel lblWorkerPort = new JLabel("Worker port: ");
        lblWorkerPort.setBounds(477, 247, 128, 16);
        contentPane.add(lblWorkerPort);

        txtMasterPort = new JTextField();
        txtMasterPort.setText("2552");
        txtMasterPort.setBounds(635, 179, 116, 22);
        contentPane.add(txtMasterPort);
        txtMasterPort.setColumns(10);

        txtWorkerAdress = new JTextField();
        txtWorkerAdress.setText("localhost");
        txtWorkerAdress.setBounds(635, 212, 116, 22);
        contentPane.add(txtWorkerAdress);
        txtWorkerAdress.setColumns(10);

        txtWorkerPort = new JTextField();
        txtWorkerPort.setText("2552");
        txtWorkerPort.setBounds(635, 244, 116, 22);
        contentPane.add(txtWorkerPort);
        txtWorkerPort.setColumns(10);

        JButton btnAddWorker = new JButton("add worker");
        btnAddWorker.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                String worker = (txtWorkerAdress.getText() + ":" + txtWorkerPort.getText());
                txtAreaWorkerList.append(worker + "\n");
                workerList.add(worker);
            }
        });
        btnAddWorker.setBounds(635, 285, 116, 23);
        contentPane.add(btnAddWorker);

        txtAreaWorkerList = new JTextArea();
        txtAreaWorkerList.setEditable(false);
        txtAreaWorkerList.setLineWrap(true);
        txtAreaWorkerList.setBounds(775, 179, 204, 129);
        contentPane.add(txtAreaWorkerList);

        JButton btnDelWorker = new JButton("delete worker");
        btnDelWorker.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent arg0) {
                txtAreaWorkerList.setText("");
                workerList.clear();
            }
        });
        btnDelWorker.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        btnDelWorker.setBounds(475, 285, 116, 23);
        contentPane.add(btnDelWorker);
    }

    public static JTextArea getInput() {
        return input;
    }

    public static JTextField getTxtMasterPort() {
        return txtMasterPort;
    }

    public static List<String> getWorkerList() {
        return workerList;
    }
}
